package com.java.controller;

import com.java.model.MediaAccount;
import com.java.model.Result;
import com.java.model.User;
import com.java.service.MediaAccountService;
import com.java.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import static org.bouncycastle.cms.RecipientId.password;


@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MediaAccountService mediaAccountService;


    /**
     * 管理员登录页面
     * @return
     */
    @GetMapping("/login")
    public String login() {
        return "/login";
    }

    /**
     * 登录
     * @param
     * @return
     */
    @PostMapping("/indexSkip")
    @ResponseBody
    public Result login(String username, String password, String status, HttpServletRequest request) {
        if ("1".equals(status)) { // 管理员
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);

//            if (list.getAdministrator() == 1) { // 超级管理员
//                request.getSession().setAttribute("type", "1"); // 存进session域中
//            } else if (list.getAdministrator() == 2) { // 管理员
//                request.getSession().setAttribute("type", "2");
//            }
            // 登录
            return userService.login(user, request);
        }else if ("2".equals(status)) { // 自媒体
            MediaAccount mediaAccount = new MediaAccount();
            mediaAccount.setUsername(username);
            mediaAccount.setPassword(password);

            return mediaAccountService.advertisingLogin(mediaAccount, request);
        }
        return null;
    }

    /**
     * 跳转到管理员
     * @return
     */
    @GetMapping("/index")
    public String index() {
        return "/index";
    }

    /**
     * 跳转到媒体主
     * @return
     */
    @GetMapping("/indexTwo")
    public String indexTwo() {
        return "/indexTwo";
    }



    /**
     * 403页面
     * @return
     */
    @GetMapping("/permissions")
    public String permissions() {
        return "403";
    }

    /**
     * 跳转到首页
     * @return
     */
    @GetMapping("/welcome")
    public String welcome(Model model, HttpServletRequest request) {
//        model.addAttribute("count", flowService.count(request));
        return "/welcome";
    }
}
